<?php
require_once(SRC_PATH."Database/DatabaseManager.php");
require_once(SRC_PATH."Object/Fetch.php");

class Processer{
	
	private $fetch;	
	
	public function run(){
		$databaseManager = new DatabaseManager();
		
		$reader = $databaseManager->read(new Fetch(), "SELECT_ONE_FETCHED");
		$reader->execute();
		$this->fetch = $reader->next();
		if($this->fetch){
			$databaseManager->update($this->fetch, "BLOCK_FETCHER");
			$this->parseTitle();
			$this->parseDescription();
			$this->parseKeywords();
			$this->parseLinks();
			$databaseManager->update($this->fetch, "FINISH_PROCESSER");						
		}
	}
	
	public function parseTitle(){
		$matches = NULL;
		if(preg_match('/<title>(.)*<\/title>/i', $this->fetch->getContent(), $matches)){
			if($matches[0]){
				$matches[0] = str_replace('<title>', '', $matches[0]);
				$matches[0] = str_replace('</title>', '', $matches[0]);				
				$this->fetch->setTitle(utf8_encode(trim($matches[0])));				
			}
		}
	}
	
	public function parseDescription(){
		$matches = NULL;		
		if(preg_match('/<meta name=\"description\" content=\"[^\"]+/i', $this->fetch->getContent(), $matches)){
			if($matches[0]){
				$this->fetch->setDescription(utf8_encode(trim(str_ireplace("<meta name=\"description\" content=\"", "", $matches[0]))));		
			}
		}
	}
	
	public function parseKeywords(){
		$matches = NULL;	
		if(preg_match('/<meta name=\"keywords\" content=\"[^\"]+/i', $this->fetch->getContent(), $matches)){
			if($matches[0]){				
				$this->fetch->setKeywords(trim(str_ireplace("<meta name=\"keywords\" content=\"", "", $matches[0])));
			}
		}
	}
	
	public function parseLinks(){
		$matches = NULL;
		if(preg_match_all('/<a\s+(?:(?:\w+\s*=\s*)(?:\w+|"[^"]*"|\'[^\']*\'))*?\s*href\s*=\s*(?<url>\w+|"[^"]*"|\'[^\']*\')(?:(?:\s+\w+\s*=\s*)(?:\w+|"[^"]*"|\'[^\']*\'))*?>.+?<\/a>/i', $this->fetch->getContent(), $matches)){
			$arrayLinks = array();
			foreach($matches[0] as $match){				
				if(!preg_match('/(nofollow|javascript:)/i', $match)){
					
					$fetch = new Fetch();
					$fetch->setTtl($this->fetch->getTtl() - 1);					
					
					if(preg_match('/href=(\'|")[^\'"]+(\'|")/i', $match, $hrefMatch)){
						if($hrefMatch[0]){
							$locationClean = trim($hrefMatch[0]);
							$fetch->setLocation(substr($locationClean, 6, strlen($locationClean) - 7));
						}
						
						$cleanAnchor = trim(preg_replace('/<a[^>]*>|<\/a[^>]*>/i', '', $match));
						if($cleanAnchor){						
							$fetch->setLinkText($cleanAnchor);
						}
						
						if(preg_match('/title=(\'|")[^\'"]+(\'|")/i', $match, $titleMatch)){
							if($titleMatch[0]){
								$titleClean = trim($titleMatch[0]);
								$fetch->setLinkTitle(substr($titleClean, 7, strlen($titleClean) - 8));
							}
						}

						if(key_exists($fetch->getLocation(), $arrayLinks)){
							$arrayLinks[$fetch->getLocation()]->setLinkText(
								$arrayLinks[$fetch->getLocation()]->getLinkText()." ".$fetch->getLinkText()
							);
							$arrayLinks[$fetch->getLocation()]->setLinkTitle(
								$arrayLinks[$fetch->getLocation()]->getLinkTitle()." ".$fetch->getLinkTitle()
							);
						}else{
							$arrayLinks[$fetch->getLocation()] = $fetch;
						}
						
					}
				}
			}

			$databaseManager = new DatabaseManager();
			foreach($arrayLinks as $fetch){
				$databaseManager->save($fetch);
			}
		}
	}	
	
	public function parseParagraphs(){
		
	}
}